const mongoose = require('mongoose');
var express = require('express')
var resumeUserInfo = express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {resumeUserInfoService } = require('../../service/impl/resumeUserInfoService/index');
const {userService } = require('../../service/impl/userService/userService');

/* 
  let fills ={
    "key1":{
      "path":"kemuid",
    },
    "key2":{
      "path":"resumeUserInfoid",
   }
  }

*/


resumeUserInfo.post('/post/resumeUserInfo/add', async (req,res) => {
  let result
  try {
    let news  = req.body.news
    result =  await resumeUserInfoService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  resumeUserInfo.post('/post/resumeUserInfo/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await resumeUserInfoService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  

  // 添加求职基本信息,这个做了特殊修改
  resumeUserInfo.post('/post/resumeUserInfo/update', async (req,res) => {
    let result
    try {
      let updates = req.body.updates
      let aa  =  await resumeUserInfoService.getIns().findOne({
        "userid":updates.userid
      })

      if(!aa){
        result = await resumeUserInfoService.getIns().create(updates)
        let ii = await userService.getIns().findOneAndUpdate({
          "_id":updates.userid
        },{
          "resumeInfo":{
            "resume":result._id,
            "isOk":true
          }
        })
      }
      else{
        result = await resumeUserInfoService.getIns().findOneAndUpdate({
          "userid":updates.userid
        },updates)
        

        let ii = await userService.getIns().findOneAndUpdate({
          "_id":updates.userid
        },{
          "resumeInfo":{
            "resume":result._id,
            "isOk":true
          }
        })


      }

 
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  resumeUserInfo.post('/post/resumeUserInfo/query', async (req,res) => {
    let result
    try {
      result =  await resumeUserInfoService.getIns().find()
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })

  // 查找用户的基本信息
  resumeUserInfo.post('/post/resumeUserInfo/queryOne', async (req,res) => {
    let result
    try {
      
      result =  await resumeUserInfoService.getIns().findOne(
        {
          "userid":req.body.conditions.userid
        }
      )
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  module.exports= resumeUserInfo